package gestion_bd;

import java.security.MessageDigest;
import java.security.cert.Certificate;

public class FingerprintCalculator {
	
	private static final char[] DIGITOS_HEXA = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	
	public FingerprintCalculator(){
		
	}
	
	public  String obtenerFingerprint(Certificate certificado){
		MessageDigest hash;
		try {
			hash = MessageDigest.getInstance("SHA-1");
			hash.update(certificado.getEncoded());
			byte[] fingerprint= hash.digest();
			return hexify(fingerprint);
		} catch (Exception e) {
			System.err.println(e.getMessage());
			return null;
		}
	}

  private  String hexify(byte bytes[]) {
	  StringBuffer buf = new StringBuffer(bytes.length * 2);

	  for (int i = 0; i < bytes.length; ++i) {
        	buf.append(DIGITOS_HEXA[(bytes[i] & 0xf0) >> 4]);
            buf.append(DIGITOS_HEXA[bytes[i] & 0x0f]);
      }

        return buf.toString();
    }
}
